import classes
from classes import User, Group, License, Photo, Tag, BaseGlittr

class Glittr(BaseGlittr):
    """
    Glittr provides a way to perform some searches not available on
    the objects representing things on Flickr (e.g. User, Photo).

    It also lets you upload photos with the .upload_photo() method on the
    current user, e.g.:

        glittr.me.upload_photo(photo)
    
    BaseGlittr, the parent class of most of the objects, has a special object
    cache that it uses to make sure that if, for instance, you get the same
    photo two different ways, it's actually the same object.

    """
    def __init__(self, flickr, caching=True):
        """
        Create a new Glittr object using `flickr` as the Flickr object used to
        interact with Flickr.

        `caching` tells Glittr if it should cache the results retrieved by
        pseudo-attributes.  E.g. when you use glittr.me.photosets

        """
        classes.FLICKR_API = flickr
        classes.CACHING = caching

    def __repr__(self):
        return "<Glittr: %s>" % str(self.flickr)

    @property
    def me(self):
        """
        Get the current user.

        """
        return self._get_current_user()

    def check_login(self):
        """
        See if the current user that 

        api: flickr.test.login

        """
        response = self._action(self.flickr.test.login, {}, ['user'])
        return self.me == User(**response)

    def find_user(self, username=None, email=None, url=None):
        """
        Search for users with any single property: username, email, or url.
        Returns at most a single user.

        """
        if username:
            r = self.flickr.people.findByUsername(username=username)
        elif email:
            r = self.flickr.people.findByEmail(find_email=email)
        elif url:
            r = self.flickr.urls.lookupUser(url=url)
        return User(**r['user'])

    def find_group(self, url):
        """
        Get the group available at the specified url.

        """
        r = self.flickr.urls.lookupGroup(url=url)
        return Group(**r['group'])

    def find_photos(self, *objects, **params):
        """
        Returns a generator of all the photos matching the search criteria.

        There's actually a 4k limit on the search results, every page after
        page 41 (using 100 per page) is the same.

        api: flickr.photos.search

        """
        return self._action(objects, params, self.flickr.photos.search, 'photo', Photo)

    def get_interesting(self, *objects, **params):
        """
        Returns a generator of the interesting photos on Flickr for a given
        date (default: today).

        api: flickr.interestingness.getList

        """
        return self._action(objects, params, self.flickr.interestingness.getList, 'photo', Photo)

    def get_recent_photos(self, *objects, **params):
        """
        Returns a generator of the latest public uploads to Flickr.

        api: flickr.photos.getRecent

        """
        return self._action(objects, params, self.flickr.photos.getRecent, 'photo', Photo)

    def find_groups(self, text=None, url=None, **params):
        """
        Returns a generator of groups with `text` in their names or
        descriptions. Or else find the group that is at the specified url.

        api: 
       
        """
        if text:
            params['text'] = text
            return self._action([], params, self.flickr.groups.search, 'group', Group)
        if url:
            params['url'] = url
            return self._action([], params, self.flickr.urls.lookupGroup, ['group'], Group)

    def get_licenses(self, *objects, **params):
        """
        Returns a generator of all available licenses.

        """
        return self._action(objects, params, self.flickr.photos.licenses.getInfo, 'license', License)

    def get_hot_tags(self, *objects, **params):
        response = self._action(objects, params, self.flickr.tags.getHotList, ['hottags','tag'])
        return (Tag(**x) for x in response)
    
